<?php
namespace WorldCup\Controller;
use Common\Controller\WorldCupController;
class IndexController extends WorldCupController{
    public $key = 'yuanyi';
    public $url = "http://oss.beikg.cn/";
    /**
     * 存放模板消息
     */
    public function send()
    {
        $form_id        =   I('post.form_id','','htmlspecialchars');
        $app            =   I('post.app','','htmlspecialchars');
        $openid         =   I('post.openid','','htmlspecialchars');
        $form_type      =   1;
        $template_id    =   1;
        $date           =   date('Y-m-d');

        if (! empty($form_id) && ! empty($openid))
        {
            if ($app == 'worldcup')
            {
                $appid  =   'wxb26efa19cb837835';
                $secret =   '86cd81876b2c203de94f6b17e6f144e9';
                $model  =   'worldcup_template_send';
            }
            else
            {
                $appid  =   'wxb2dfa5282782f280';
                $secret =   'a814e9599f2da97118888ebcb447d3f8';
                $model  =   'worldcup_template_send';
            }

            $insert_data = array(
                'appid'=>$appid,
                'template_id'=>$template_id,
                'page'=>'pages/index/index',
                'form_id'=>$form_id,
                'form_type'=>$form_type,
                'status'=>'-1',
                'add_time'=>time(),
                'secret'=>$secret,
                'createtime'=>$date,
                'touser'=>$openid
            );

            M('worldcup_template_send')->data($insert_data)->add();
        }

    }

    /**
     * 广告
     */
    public function advertising()
    {
        $data = M('newsong_ad')->where(array('isshow'=>'1'))->select();

        foreach ($data as $key=>$val){

            $data[$key]['logo'] = $this->url.$data[$key]['logo'];

        }

        $this->returnData($data,$msg='ok',$code=200);
    }

    /**
     * 跟多好玩
     */
    public function more()
    {
        $data['appid']  =   'wx6b1c7907c5733fc9';
        $data['path']   =   '/pages/index/index';
        $this->returnData($data,$msg='ok',$code=200);
    }

    public function login()
    {
        $code                   =  I('post.code','','htmlspecialchars');
        $app                    =  I('post.app','','htmlspecialchars');

        if (! empty($code))
        {
            if ($app == 'worldcup')
            {
                $appid  =   'wxb26efa19cb837835';
                $secret =   '86cd81876b2c203de94f6b17e6f144e9';
            }
            else
            {
                $appid  =   'wxb2dfa5282782f280';
                $secret =   'a814e9599f2da97118888ebcb447d3f8';
            }


            $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

            $res = $this->curlget($url);

            if (!$res['openid'])
            {
                $this->returnData($res,$msg='openid获取失败',$code=1001);
            }

            $data               =   array();
            $data['openid']     =   $res['openid'];
            $this->returnData($data,'ok',0);
        }
    }
}